package ui.adapter;

import android.app.Activity;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fy.fyzf.R;
import com.fy.fyzf.bean.CategoryBean;
import com.fy.fyzf.listener.OnCallbackListener;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;


public class CategoryAdapter extends BaseQuickAdapter<CategoryBean, BaseViewHolder> {
    public static final int SHOW_LAB_NUM = 7;//每个item下展示几个标签

    Activity activity;
    boolean isSelectOne;
    OnCallbackListener listener;

    public CategoryAdapter(Activity activity, List<CategoryBean> data, boolean isSelectOne, OnCallbackListener listener) {
        super(R.layout.item_lab_title, data);
        this.activity = activity;
        this.listener = listener;
        this.isSelectOne = isSelectOne;
    }

    @Override
    protected void convert(final BaseViewHolder helper, CategoryBean item) {
        helper.setText(R.id.tvTitle, item.getTypename());
        final TagFlowLayout flowLayout = helper.getView(R.id.flowlayout);//子类别
        final List<CategoryBean.ChildBean> child = item.getChild();
        final int labLen = child.size();
        LabAdapter labAdapter = new LabAdapter(child, activity);
        flowLayout.setAdapter(labAdapter);
        flowLayout.setMaxSelectCount(1);
        flowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                CategoryBean.ChildBean bean = child.get(position);
//                bean.setIsCheck(!bean.isIsCheck());
                HashMap<Integer, Boolean> map = new HashMap<>();
                map.put(position, true);
                bean.setMap(map);
                //这里有问题
                //选中或者取消选中
                if (bean.isIsCheck()) {
                    listener.onUnSelected(helper.getAdapterPosition(), position);
                } else {
                    listener.onSelected(helper.getAdapterPosition(), position);
                }
                return false;
            }
        });
        HashSet<Integer> posSet = new HashSet<>();
        for (int i = 0; i < labLen; i++) {//判断是否被选中5
            CategoryBean.ChildBean childBean = child.get(i);
            if (childBean.getMap().get(i)) {
                posSet.add(i);
            }
            labAdapter.setSelectedList(posSet);
        }
    }
}
